import json
import os

from tqdm import tqdm


def traverse_dir(path, tails, file_info_ls=None):
    """
    :param path:
    :param tails:
    :param file_info_ls:
    :return: [(文件路径1，文件名1),(文件路径2，文件名2),(文件路径3，文件名3),...,(文件路径n，文件名n)]
    注意：如路经太深python就无法检测该文件了，遇到这种情况请开启 clean_path功能；
    参考： https://blog.csdn.net/baidu_34928905/article/details/131935428
    """
    if file_info_ls is None:
        file_info_ls = []
    for file in os.listdir(path):
        file_path = os.path.join(path, file)
        if os.path.isdir(file_path):
            traverse_dir(file_path, tails, file_info_ls)
        else:
            file_e_ls = os.path.basename(file_path).split('.')
            file_tail = file_e_ls[-1]
            if (tails is None or file_tail.lower() in tails) and not os.path.basename(file_path).startswith('~$'):
                item = (file_path, '.'.join(file_e_ls[0:-1]))
                file_info_ls.append(item)
    return file_info_ls


def parse_txt(failed_ls, file_dir, out_dir):
    file_info = traverse_dir(file_dir, ['txt'])
    if not file_info:
        return
    file_ls = []
    for fp, f_uuid in tqdm(file_info, desc='正在提取txt文件'):
        try:
            with open(fp, 'r', encoding='utf-8') as file:
                content = file.read()
                file_dict = {'file_path': fp, 'file_uuid': f_uuid, 'image_info': {}, 'table_info': {},
                             'content': content}
                file_ls.append(file_dict)
        except Exception as e:
            failed_ls.append({'filePath': fp, 'reason': str(e)})
    if file_ls:
        with open(out_dir + '/txt_content.json', 'w+', encoding='utf-8') as fp:
            data = {'file_num': len(file_ls), 'file_suffix': 'txt', 'file_list': file_ls}
            json.dump(data, fp, ensure_ascii=False)
